package com.example.lin.ximalaya.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.lin.ximalaya.R;
import com.example.lin.ximalaya.adapter.HotAdapter;
import com.example.lin.ximalaya.pojo.HotData;
import com.example.lin.ximalaya.utils.HttpUtils;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecyclerViewFragment extends Fragment {


    @BindView(R.id.rv_home)
    RecyclerView rvHome;
    Unbinder unbinder;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recycler_view, container, false);
        unbinder = ButterKnife.bind(this, view);
        initView();


        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        Bundle arguments = getArguments();
        int anInt = arguments.getInt("index");
        if (anInt == 1) {
            loadData();
        }
    }

    private static final String TAG = "MainActivity";
    private HotAdapter hotAdapter;

    private void initView() {
        rvHome.setLayoutManager(new LinearLayoutManager(getActivity()));
        hotAdapter = new HotAdapter(getActivity());
        rvHome.setAdapter(hotAdapter);
    }


    private Handler h = new Handler(new Handler.Callback() {
        /**
         * 接受处理消息
         * @param msg
         * @return
         */
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 1://消息what==1
                    //获取消息绑定的 obj 对象
                    HotData hotData = (HotData) msg.obj;
                    //绑定数据到适配器
                    hotAdapter.setHotData(hotData);
                    break;
            }
            return false;
        }
    });

    private void loadData() {
        new Thread(new Runnable() {
            @Override
            public void run() {


                String s1 = HttpUtils.get("http://mobile.ximalaya.com/discovery-firstpage/v2/explore/ts-1536547537960?appid=0&categoryId=-2&code=43_440000_4403&device=android&deviceId=57cf4217-4dcc-364b-9845-40eaa02775bc&includeActivity=true&includeSpecial=true&inreview=false&network=wifi&operator=2&pullToRefresh=true&scale=1&version=6.5.16");
                Log.d(TAG, "run: " + s1);
                String s = HttpUtils.get(HttpUtils.URL_RECOMMEND);
                //解析json
                HotData hotData = new Gson().fromJson(s, HotData.class);

                Message message = h.obtainMessage(1, hotData);//创建消息,what=1,obj=hotData

                h.sendMessage(message);//发送消息
            }
        }).start();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
